﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TomNet.IMSC.Core.Contracts.Common;
using TomNet.IMSC.Model.DbModels.Common;
using TomNet.Web.Mvc;
using TomNet.Web.Mvc.UI;

namespace TomNet.IMSC.Web.Controllers
{
    public class ImagesController : BaseController
    {
        public IImagesContract ImagesContract { get; set; }
        public IImageGroupContract IGContract { get; set; }
        #region 相册列表
        // GET: Images
        [Description("管理-相册列表")]
        public ActionResult Index(int GroupID = 0, int InterfixID = 0, int ImageType = 0)
        {
            ViewBag.GroupID = GroupID;
            ViewBag.InterfixID = InterfixID;
            ViewBag.ImageType = ImageType;
            var data = ImagesContract.Entities.Where(m => !m.IsDeleted);

            if (GroupID != 0)
            {
                data = data.Where(m => m.GroupID.Equals(GroupID));
            }
            if (InterfixID != 0)
            {

                data = data.Where(m => m.InterfixID.Equals(InterfixID));
            }
            if (ImageType != 0)
            {
                data = data.Where(m => m.ImageType.Equals(ImageType));
            }


            IList<string> listPath = new List<string>();
            IList<string> listId = new List<string>();


            foreach (var item in data)
            {
                listPath.Add(item.ImagePath + item.FileName);
                listId.Add(item.Id.ToString());
            }
            ViewBag.listPath = listPath;
            ViewBag.listId = listId;
            return View();
        }
        #endregion
        #region 图片上传
        [Description("管理-图片上传")]
        public ActionResult ProcessImgUpload()
        {

            var result = new AjaxResult();
            NameValueCollection nvc = System.Web.HttpContext.Current.Request.Form;
            HttpFileCollection hfc = System.Web.HttpContext.Current.Request.Files;
            
            if (hfc.Count > 0)
            {
                #region 多张图片上传
                for (int i = 0; i < hfc.Count; i++)
                {
                    string[] fn = hfc[i].FileName.Split('.');
                    if (fn[fn.Length - 1].ToUpper() == "JPG" || fn[fn.Length - 1].ToUpper() == "GIF" || fn[fn.Length - 1].ToUpper() == "PNG")
                    {
                        string SavePath = Server.MapPath("/Images/" + IGContract.GetByKey(Convert.ToInt32(nvc.Get("GroupID"))).ImagePath + "/");
                        if (!Directory.Exists(SavePath))
                        {
                            Directory.CreateDirectory(SavePath);
                        }
                        string FileName = DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + "." + fn[fn.Length - 1];
                        while (System.IO.File.Exists(SavePath + FileName))
                        {
                            System.Threading.Thread.Sleep(1);
                            FileName = DateTime.Now.ToString("yyyyMMddHHmmssfffffff") + "." + fn[fn.Length - 1];
                        }
                        hfc[i].SaveAs(SavePath + FileName);
                        Images im = new Images();
                        im.CreatedTime = DateTime.Now;
                        im.FileName = FileName;
                        im.GroupID = Convert.ToInt32(nvc.Get("GroupID"));
                        im.ImagePath = "/Images/" + IGContract.GetByKey(Convert.ToInt32(nvc.Get("GroupID"))).ImagePath + "/";
                        im.ImageType = Convert.ToInt32(nvc.Get("ImageType"));
                        im.InterfixID = Convert.ToInt32(nvc.Get("InterfixID"));
                        im.IsDeleted = false;
                        im.IsLocked = false;
                        im.Remark = "";
                        im.ShowIndex = 0;
                        im.Tag = "";
                        try
                        {
                            var count = ImagesContract.Insert(im);
                            if (count > 0)
                            {
                                result.Type = AjaxResultType.Success;
                                result.Content = "录入成功";
                            }
                            else
                            {
                                result.Type = AjaxResultType.Error;
                                result.Content = "录入失败";
                            }
                        }
                        catch
                        {
                            result.Type = AjaxResultType.Error;
                            result.Content = "异常操作";
                        }
                    }
                    else
                    {
                        result.Type = AjaxResultType.Error;
                        result.Content = "图片格式不正确";
                    }
                }
                #endregion
                 
            }
            else
            {
                result.Type = AjaxResultType.Error;
                result.Content = "录入失败";
            }
            return Json(result);
        }
        #endregion 
        #region 图片删除
        [Description("管理-图片删除")]
        [HttpPost]
        public ActionResult DeleteAsync(string ids)
        {
            var result = new AjaxResult();
            try
            {
                string[] idsStr = ids.Split(new char[] { ',' });
                int[] idsInt = Array.ConvertAll(idsStr, id => Convert.ToInt32(id));
                ImagesContract.UpdateDirect(m => idsInt.Contains(m.Id), m => new Images { IsDeleted = true });

                result.Type = AjaxResultType.Success;
                result.Content = "删除成功";
            }
            catch
            {
                result.Type = AjaxResultType.Error;
                result.Content = "异常操作";
            }
            return JsonEx(result);
        }
        #endregion
    }
}